<?php
class BuyService {
	
	private $_num = 200; //每页显示200天
	private $_module = array (1 => "按件数", 2 => "外径长宽", 3 => "内径长宽", 4 => "片数", 5 => "面积" );
	
	private $_status = array (0 => "正常", - 2 => "停产", - 1 => "抢完了", 2 => "库存紧张", 3 => "限时特价" );
	
	public function listing() {
		$catetory = $this->getCategory ();
		return array ('category' => $catetory );
	}
	
	public function addGoods($params) {
		$rs = $this->getGoodsByName ( $params ['goods_name'] );
		if (! empty ( $rs )) {
			return "产品名字重复了";
		}
		
		$flag = BuyModel::instance ()->addGoods ( $params );
		return $flag > 0 ? "新增成功" : "新增失败";
	}
	
	public function getGoodsBySales() {
		
		$kb = BuyModel::instance ()->getGoodsKBBySales ( 10 );
		return $this->parseGoodsStatus ( $kb );
	
	}
	
	//推荐产品
	public function getRecommendGoods() {
		$rs = BuyModel::instance ()->getGoodsByRecommend ();
		if (empty ( $rs )) {
			return;
		}
		return $this->getGoodsImageBySKU ( $rs );
	
	}
	
	//获取图片
	private function getGoodsImageBySKU($rs) {
		
		foreach ( $rs as $k => $v ) {
			$goods = GoodsModule::instance ()->getConBySku ( $v ['sku'] );
			$rs [$k] ['image'] = $goods ['image'];
		}
		
		return $rs;
	}
	
	public function getGoodsByCid($cid) {
		$rs = BuyModel::instance ()->getGoodsByCid ( 0, $this->_num, $cid );
		$rs = $this->parseGoodsStatus ( $rs );
		$cate = $this->getCateById ( $cid );
		$cart = $this->parseCOOKIE ();
		$rs = $this->getGoodsImageBySKU ( $rs );

		if (empty ( $cart )) {
			return array ('goods' => $rs, 'cate' => $cate );
		}
		
		foreach ( $cart as $v ) {
			$carts [] = $v ['id'];
		}
		foreach ( $rs as $k => $v ) {
			//加入图片
			

			if (in_array ( $v ['id'], $carts )) {
				$rs [$k] ['buy'] = 1;
			} else {
				$rs [$k] ['buy'] = 0;
			}
		
		}
		
		return array ('goods' => $rs, 'cate' => $cate );
	}
	
	//处理产品状态
	private function parseGoodsStatus($goods) {
		
		foreach ( $goods as $k => $v ) {
			
			$goods [$k] ['flag'] = $this->_status [$v ['status']];
		}
		
		return $goods;
	}
	
	public function getCateById($cid) {
		return BuyCateModel::instance ()->getCateById ( $cid );
	}
	
	public function getGoodsByName($goodsName) {
		return BuyModel::instance ()->getGoodsByName ( $goodsName );
	}
	
	public function getGoodsByRemark($remark) {
		$remark = urldecode ( $remark );
		$rs = BuyModel::instance ()->getGoodsByRemark ( $remark );
		return $this->parseGoodsStatus ( $rs );
	}
	
	public function getCart() {
		$rs = $this->parseCOOKIE ();
		$total = 0; //订单总额
		$count = 0; //商品数
		

		if (empty ( $rs )) {
			return array ('goods' => $rs, 'total' => $total );
		}
		foreach ( $rs as $k => $v ) {
			
			$goods = BuyModel::instance ()->getGoodsById ( $v ['id'] );
			//过滤掉已经停产或者售罄的产品
			if ($goods ['status'] < 0) {
				continue;
			}
			
			$cate = $this->getCateById ( $goods ['cid'] );
			$rs [$k] ['sku'] = $goods ['sku'];
			$rs [$k] ['price'] = empty ( $v ['snapshot'] ) ? $goods ['price'] : $v ['price'];
			
			$rs [$k] ['goods_name'] = $goods ['goods_name'];
			$rs [$k] ['type'] = $goods ['type'];
			$rs [$k] ['cid'] = $goods ['cid'];
			$rs [$k] ['remark'] = $goods ['remark'];
			$rs [$k] ['count'] = $v ['count'];
			$rs [$k] ['module'] = $goods ['module'];
			//检查快照
			$snapshot = $goods ['type'] . " " . $goods ['num'] . $cate ['unit'] . '/件';
			$rs [$k] ['snapshot'] = empty ( $v ['snapshot'] ) ? $snapshot : $v ['snapshot'];
			$rs [$k] ['num'] = $goods ['num'];
			$rs [$k] ['unit'] = $cate ['unit'];
			$count += $v ['count'];
			$total += $rs [$k] ['price'] * $goods ['num'] * $v ['count'];
		}
		return array ('goods' => $rs, 'total' => $total, 'count' => $count );
	}
	
	private function parseCOOKIE() {
		$cookie = $_COOKIE ['buy'];
		$goods = json_decode ( $cookie );
		$rs = "";
		$i = 0;
		
		if (empty ( $goods )) {
			return;
		}
		
		foreach ( $goods as $k => $v ) {
			
			if (empty ( $v->count )) {
				continue;
			}
			
			$rs [$i] ['id'] = $k;
			$rs [$i] ['count'] = $v->count;
			$rs [$i] ['goods_name'] = $v->name;
			$rs [$i] ['price'] = $v->price;
			$rs [$i] ['snapshot'] = $v->snapshot;
			$i ++;
		}
		
		return $rs;
	}
	
	public function getCategory() {
		return BuyCateModel::instance ()->getCate ();
	}
	
	public function getGoodsBySKU($sku) {
		if (empty ( $sku ) || $sku == "NULL") {
			return;
		}
		$base = $this->getGoodsBase ( $sku );
		$comment = $this->getGoodsComment ( $base ['nid'] );
		$desc = $this->getGoodsDesc ( $base ['nid'] );
		return array ('base' => $base, 'desc' => $desc, 'comment' => $comment );
	}
	
	public function getGoodsStatus() {
		return $this->_status;
	}
	
	public function getGoodsModule() {
		return $this->_module;
	}
	
	public function getGoodsById($goodsId) {
		return BuyModel::instance ()->getGoodsById ( $goodsId );
	}
	
	public function getGoodsLikeTitle($title) {
		if (empty ( $title )) {
			return;
		}
		
		$title = urldecode ( $title );
		$rs = BuyModel::instance ()->getGoodsLikeTitle ( $title );
		return $this->parseGoodsStatus ( $rs );
	}
	
	public function getGoodsAPI($title) {
		if (empty ( $title )) {
			return;
		}
		
		$title = urldecode ( $title );
		$rs = BuyModel::instance ()->getGoodsLikeTitle ( $title );
		$arr = array ();
		if (empty ( $rs )) {
			return;
		}
		foreach ( $rs as $k => $v ) {
			if ($k > 10) {
				break;
			}
			$arr [$k] ['label'] = $v ['goods_name'];
		}
		return $arr;
	}
	
	public function saveGoodsById($value) {
		$id = $_POST ['id'];
		unset ( $_POST ['id'] );
		$data = $_POST;
		
		$data ['recommend'] = ! isset ( $data ['recommend'] ) ? 0 : 1;
		$flag = BuyModel::instance ()->setGoodsById ( $data, $id );
		if ($flag > 0) {
			return "更新成功";
		} else {
			return "更新失败";
		
		}
	}
	
	//产品销量
	public function setGoodsSalesById($num, $id) {
		return BuyModel::instance ()->setGoodsSalesById ( $num, $id );
	}
	
	private function getGoodsBase($sku) {
		return GoodsModule::instance ()->getConBySku ( $sku );
	}
	
	//获取宝贝描述
	private function getGoodsDesc($id) {
		return ArticleModule::instance ()->getCon ( $id );
	}
	
	private function getGoodsComment($nid) {
		$comment = new CommentService ();
		return $comment->getCommentByNid ( $nid, 10 );
	}
}